package items100;

public class Item61 {
	public static void main(String[] args){
		int[] arr = {2, 4, 1, 16, 7, 5, 11, 9};
		findMaxMinus(arr);
		findMaxMinus_pro(arr);
		
		
	}
	
	public static void findMaxMinus(int[] arr){
		if(arr.length<=0)
			return;
		int indexOfMax = 0;// indexOfMaxSecond>= indexOfMaxFirst
		int value = 0;
		for(int i=0;i<arr.length;i++){
			if(arr[indexOfMax]-arr[i] > value){
				value = arr[indexOfMax]-arr[i];
			}
			else if(arr[i]> arr[indexOfMax]){
				indexOfMax = i;
			}
		}
		System.out.println("value="+value);
		
	}
	
	
	public static void findMaxMinus_pro(int[] arr){
		if(arr.length<=0)
			return;
		int indexOfMax = 0;
		int firstIndex = 0;
		int secondIndex = 0;
		int value = 0;
		for(int i=0;i<arr.length;i++){
			if(arr[indexOfMax]-arr[i] > value){
				value = arr[indexOfMax]-arr[i];
				secondIndex = i;
				firstIndex = indexOfMax;
			}
			else if(arr[i] > arr[indexOfMax]){
				indexOfMax = i;

			}
		}
		System.out.println("value=" + value);
		System.out.println("first index =" + firstIndex);
		System.out.println("secondIndex index =" + secondIndex);
		
	}

}
